<?php

namespace CNEPAdmin\Controller;

use CNEPAdmin\Controller\CommonController;

class SlideImgController extends CommonController {

    protected $_modelName = "SlideImg";

    protected function setCode() {
        $this->_code = "System"; // 属于系统管理模块
    }

    public function index() {
        $this->display();
    }

	
	protected function _getQueryArr(&$where, &$order) {

        if (intval(trim(I("sType", -1))) >= 0) {
        	$where['type'] = I("sType");
        }

        $where['isOn'] = 1;

        
		//dump($where);
        return $where;
    }
	
	
    /**
     * 查看列表
     */
    public function slideImgList() {
        $this->checkPriv('slideImg', 1, false);

        $this->_setModel();
        $rt = $this->_model->getSlideImgList($this->_getQueryArr());
        $this->assign('imgDomain',C('IMG_DOMAIN'));
        $this->assign('list', $rt);
        $this->display();
    }

    /**
     *  添加
     */
    public function addSlideImg() {
        $this->checkPriv('slideImg', 2);
        
        $shopID = I('shopID', -1);
        $goodID = I('goodsID', -1);
		$moduleID = I('moduleID', -1);
		$materialID = I('materialID', -1);
        
        if($shopID > 0) {
            $shopArr = D("Shop")->where('isOn = 1 and shopID = ' . $shopID)->field('shopType')->find();
            $_POST['shopType'] = $shopArr['shopType'];
        }
        if($goodID > 0) {
            $goodArr = D('Goods')->where("isOn = 1 and goodsID = $goodID")->field("shopID")->find();
            if($goodArr && $goodArr['shopID'] > 0) {
                $shopArr = D("Shop")->where('isOn = 1 and shopID = ' . $goodArr['shopID'])->field('shopType')->find();
                $_POST['shopType'] = $shopArr['shopType'];
            }
        }
        !empty($_POST['imgPath'])?$_POST['imgPath']=$this->get_url_path(I('post.imgPath')):"";
        !empty($_POST['thumb'])?$_POST['thumb']=$this->get_url_path(I('post.thumb')):"";
        $this->_ajaxOperate('addSlideImg');
    }

    /**
     * 删除
     */
    public function deleteSlideImg() {
        $this->_ajaxOperate( 'deleteSlideImg','slideImg' , 2, I("post.imgID"));
    }

    /**
     * 查看
     */
    public function slideImgMsg() {
        $this->checkPriv('slideImg', 1, false);

        $id = intval(I("imgID", 0));

        if ($id > 0) { // 编辑
            $this->_setModel();
            $this->assign("arr", $this->_model->getSlideImg($id));
        }

        $this->display();
    }

    /**
     * 更新基本信息(需要更新什么就提交什么)
     */
    public function updateSlideImg() {
        $this->checkPriv('slideImg', 2);
        
        $shopID = I('shopID', -1);
        $goodsID = I('goodsID', -1);
        
        if($shopID > 0) {
            $shopArr = D("Shop")->where('shopID = ' . $shopID)->field('shopType')->find();
            $_POST['shopType'] = $shopArr['shopType'];
        }
        if($goodsID > 0) {
            $goodArr = D('Goods')->where("isOn = 1 and goodsID = $goodsID")->field("shopID")->find();
            if($goodArr && $goodArr['shopID'] > 0) {
                $shopArr = D("Shop")->where('isOn = 1 and shopID = ' . $goodArr['shopID'])->field('shopType')->find();
                $_POST['shopType'] = $shopArr['shopType'];
                $_POST['shopID'] = $goodArr['shopID'];
            }
        }
        !empty($_POST['imgPath'])?$_POST['imgPath']=$this->get_url_path(I('post.imgPath')):"";
        !empty($_POST['thumb'])?$_POST['thumb']=$this->get_url_path(I('post.thumb')):"";
        $this->_ajaxOperate('updateSlideImg');
    }

}
